package MainPackage;

import org.apache.commons.lang.StringUtils;

public class Interpereter {
	final static String CLASS = "class";
	public TranslateFile translator = new TranslateFile();

	final static String[] badThings = new String[] { "\n", "\t", "\r" };

	public static String removeAllBadThings(String javaString) {
		for (String bad : badThings) {
			javaString = javaString.replace(bad, "");
		}
		return javaString;
	}

	// These are only intended to work in sequential order at the moment, can be
	// changed at any time
	public String interperet(String javaString) {
		javaString = removeAllBadThings(javaString);
		javaString = StringUtils.stripEnd(javaString, null);
		String[] javaWords = javaString.split(" ");
		if ("}".equals(javaString)) {
			return translator.pullBottomDepthString();
		} else if ("import".equals(javaWords[0])
				|| "package".equals(javaWords[0])) {
			return "";
		} else if (isAssignment(javaWords)) {
			return translator.translateAssignment(javaString);
		} else if (isClassType(javaString)) {
			return translator.translateClassString(javaString);
		} else if (isLocalVariable(javaWords)) {
			return translator.translateLocalVariableString(javaString);
		} else if (isIfStatement(javaString)) {
			return translator.translateIfStatement(javaString);
		} else if (isElseStatement(javaString)){
			return translator.translateElseStatement(javaString);
		} else if (isMethodType(javaString)) {
			return translator.translateMethodLine(javaString);
		} else if (isReturnStatement(javaWords)) {
			return translator.translateReturnStatement(javaString);
		}

		return "";
	}

	private boolean isElseStatement(String javaString) {
		if(javaString.contains("else") && !javaString.contains("else if"))
			return true;
		return false;
	}

	private boolean isAssignment(String[] javaWords) {
		if(javaWords.length >= 2 && "=".equals(javaWords[1]))
			return true;
		return false;
	}

	private static boolean isReturnStatement(String[] javaWords) {
		return "return".equals(javaWords[0]);
	}

	private static boolean isMethodType(String javaString) {
		if (javaString.contains(")") && javaString.contains("{"))
			return true;
		return false;
	}

	private static boolean isIfStatement(String javaString) {
		if (javaString.contains("if(") || javaString.contains("if ("))
			return true;
		return false;
	}

	private static boolean isLocalVariable(String[] javaWords) {
		if (!"return".equals(javaWords[0])
				&& (javaWords.length == 2 || javaWords.length > 2
						&& "=".equals(javaWords[2])))
			return true;
		return false;
	}

	private static boolean isClassType(String javaString) {
		if (javaString.contains(CLASS))
			return true;
		return false;
	}

}
